import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:management/model/user_model.dart';

///注册账号
class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  GlobalKey<FormState> _formKey = GlobalKey();
  TextEditingController _nameController = TextEditingController();
  TextEditingController _pswController = TextEditingController();
  TextEditingController _pswAgainController = TextEditingController();

  @override
  void initState() {
    SystemChrome.setEnabledSystemUIOverlays([]);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("注册"),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Container(
          height: 500,
          child: Form(
            key: _formKey,
            autovalidateMode: AutovalidateMode.onUserInteraction,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  height: 120,
                  alignment: Alignment.center,
                  child: TextFormField(
                    style: TextStyle(fontSize: 30),
                    validator: _validatorUserName,
                    controller: _nameController,
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.person),
                      hintText: "请输入用户名",
                    ),
                  ),
                ),
                Container(

                  height: 120,
                  alignment: Alignment.center,
                  child: TextFormField(
                    validator: _validatorPsw,
                    style: TextStyle(fontSize: 30),
                    controller: _pswController,
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.lock),
                      hintText: "请输入密码",
                    ),
                    obscureText: true,
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  height: 120,
                  alignment: Alignment.center,
                  child: TextFormField(
                    validator: _validatorPsw,
                    style: TextStyle(fontSize: 30),
                    controller: _pswAgainController,
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.lock),
                      hintText: "请确认密码",
                    ),
                    obscureText: true,
                  ),
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(top: 50),
                    width: 300,
                    child: MaterialButton(
                      color: Colors.blue,
                      onPressed: () {
                        confirm();
                      },
                      child: Text(
                        "确认",
                        style: TextStyle(color: Colors.white, fontSize: 30),
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }



  void confirm() async {
    _formKey.currentState!.save();
    if (_formKey.currentState!.validate()) {
      FocusScope.of(context).requestFocus(FocusNode());
      String userName = _nameController.text;
      User? user = await UserProvider.instance.queryName(userName);

      if (user != null && user.name == userName) {
        Fluttertoast.showToast(msg: "用户名已存在");
        return;
      }
      String psw = _pswController.text;
      String pswAgain = _pswAgainController.text;
      if (psw != pswAgain) {
        Fluttertoast.showToast(msg: "密码不一致");
        return;
      }
      UserProvider.instance.insert(User(userName, psw));
      Navigator.of(context).pop();
    }
  }

  String? _validatorUserName(String? value) {
    if (value!.isEmpty) {
      return "用户名不能为空";
    }

    // var regExp = RegExp(RegexUtil.regexZh);
    // if (!regExp.hasMatch(value)) {
    //   return "必须为汉字";
    // }
    return null;
  }

  String? _validatorPsw(String? value) {
    if (value!.isEmpty) {
      return "密码不能为空";
    } else {
      return null;
    }
  }
}
